#!/bin/bash

# 更新系统
sudo apt update
sudo apt upgrade -y

# 安装依赖
sudo apt install -y python3-pip python3-venv tesseract-ocr tesseract-ocr-chi-sim nginx

# 创建项目目录
sudo mkdir -p /opt/pdf-toolkit
sudo chown -R ubuntu:ubuntu /opt/pdf-toolkit

# 创建虚拟环境
python3 -m venv /opt/pdf-toolkit/venv
source /opt/pdf-toolkit/venv/bin/activate

# 安装依赖包
pip install -r requirements.txt

# 创建日志目录
mkdir -p /opt/pdf-toolkit/logs

# 配置 Nginx
sudo tee /etc/nginx/sites-available/pdf-toolkit <<EOF
server {
    listen 80;
    server_name your_domain.com;

    location / {
        proxy_pass http://0.0.0.0:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
        proxy_buffering off;
        proxy_read_timeout 300s;
    }
}
EOF

# 启用 Nginx 配置
sudo ln -s /etc/nginx/sites-available/pdf-toolkit /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

# 配置 systemd 服务
sudo cp pdf-toolkit.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable pdf-toolkit
sudo systemctl start pdf-toolkit 